﻿using System;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;

namespace ContractManagementSystem
{
    public partial class PageReportContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewPanel.Visible = true;
                EditPanel.Visible = false;
                FetchAll();
            }
        }

        private void FetchAll()
        {
            if (Session["Name"] != null)
            {
                var sessionUser = Session["Name"].ToString();
                if (sessionUser.Equals("Admin"))
                {
                    var data = ContractBusiness.FetchAll();
                    Session["ReportContract"] = ContractBusiness.FetchAll();
                    ContractListView.DataSource = data;
                }
                else if (sessionUser.Equals("Consultant"))
                {
                    var id = UserBusiness.FetchIdByUserName(sessionUser);
                    var data = ContractBusiness.FetchByConsultantId(id);
                    Session["ReportContract"] = ContractBusiness.FetchByConsultantId(id);
                    ContractListView.DataSource = data;
                }
                else if (sessionUser.Equals("LineManager"))
                {
                    var id = UserBusiness.FetchIdByUserName(sessionUser);
                    var data = ContractBusiness.FetchByManagerId(id);
                    Session["ReportContract"] = ContractBusiness.FetchByManagerId(id);
                    ContractListView.DataSource = data;
                }
                else
                {
                    var id = UserBusiness.FetchIdByUserName(sessionUser);
                    var data = ContractBusiness.FetchByContractorId(id);
                    Session["ReportContract"] = ContractBusiness.FetchByContractorId(id);
                    ContractListView.DataSource = data;
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }

            ContractListView.DataBind();

            var list = ContractTypeBusiness.FetchAll();
            ContractTypeViewDropDownList.DataSource = list;
            ContractTypeViewDropDownList.DataTextField = "Name";
            ContractTypeViewDropDownList.DataValueField = "ContractTypeId";
            ContractTypeViewDropDownList.DataBind();
            ContractTypeViewDropDownList.Items.Insert(0, "All");
        }

        protected void DataPagerListPage(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ContractorNameTextBox.Text))
            {
                if (ContractTypeViewDropDownList.Text == "All")
                {
                    FetchAll();
                }
                else
                {
                    FetchAllByContractType(ContractTypeViewDropDownList.SelectedValue);
                }
            }
            else
            {
                Search(ContractorNameTextBox.Text);
            }
        }

        private void Search(string fullName)
        {
            var data = ContractBusiness.FetchAllWithContractor(fullName);
            ContractListView.DataSource = data;
            ContractListView.DataBind();
            Session["ReportContract"] = ContractBusiness.FetchAllWithContractor(fullName);
        }

        private void FetchAllByContractType(string typeId)
        {
            var data = ContractBusiness.FetchAllWithContractType(new Guid(typeId));
            ContractListView.DataSource = data;
            ContractListView.DataBind();
            Session["ReportContract"] = ContractBusiness.FetchAllWithContractType(new Guid(typeId));
        }

        protected void SearchButton_OnClick(object sender, EventArgs e)
        {
            Search(ContractorNameTextBox.Text);
        }

        protected void ContractTypeViewDropDownList_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ContractTypeViewDropDownList.Text == "All")
            {
                FetchAll();
            }
            else
            {
                FetchAllByContractType(ContractTypeViewDropDownList.SelectedValue);
            }
        }

        protected void ReportButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ReportContract.aspx");
        }
    }
}
